package com.spring.datasource.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.ttl.threadpool.TtlExecutors;
import com.spring.datasource.dao.MCodeDao;
import com.spring.datasource.util.DynamicDataSourceHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@RestController
public class DController implements DefaultController, ApplicationContextAware {
	Logger log = LoggerFactory.getLogger(DController.class);
	@Autowired
	MCodeDao mCodeDao;

    ApplicationContext applicationContext;

    public Executor executor = Executors.newFixedThreadPool(5);


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
	public void d() {
		log.info(JSON.toJSONString(mCodeDao.getJobCodeMaster()));
		log.info(JSON.toJSONString(mCodeDao.getSmsCodeMaster()));
		Thread thread = new Thread(() -> {
            String verifyCode = DynamicDataSourceHolder.getThreadLocal();
            MDC.put("mdc_trace_id", verifyCode);
		    log.info("eee");
        });
//        try {
//            thread.start();
//            thread.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        executor.execute(thread);

        this.applicationContext.getBean(DController.class).m();
	}

	public void m() {

    }
}
